package com.fyl.verify.service;

import com.alibaba.fastjson.JSONObject;
import com.fyl.core.advance.constant.IResponseEnum;
import org.springframework.http.HttpStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@FunctionalInterface
public interface VerifyService {

    IResponseEnum authVerifyHandler(HttpServletRequest request, HttpServletResponse response);

    default void preVerifyHandler(HttpServletRequest request) {
    }

    default void postVerifyHandler(HttpServletResponse response) {
    }

    default void verifyErrorHandler(IResponseEnum responseEnum, HttpServletResponse response) throws IOException {
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpStatus.OK.value());
        PrintWriter writer = response.getWriter();
        writer.print(JSONObject.toJSON(responseEnum));
        writer.close();
        response.flushBuffer();
    }

}
